<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 150px;
            height: 150px;
            background-color: orange;
            border-radius: 50%;
        }
    </style>
</head>

<body>
    <div class="box"></div>
    <script>
        let box = document.querySelector(".box");
        let moveX = 0;
        let moveY = 0;
        // let startX = 0;
        // let startY = 0;
        // let stopX = 0;
        // let stopY = 0;
        //记录触摸球的开始坐标
        let startX = 0;
        let startY = 0;
        box.addEventListener("touchstart", function (event) {
            startX = event.touches[0].pageX;
            startY = event.touches[0].pageY;

        })
        //球移动
        box.addEventListener("touchmove", function (event) {
            //当前小球停留的坐标+手指移动的坐标—小球刚开始的坐标
            moveX = stopX + event.touches[0].pageX - startX;
            moveY = stopY + event.touches[0].pageY - startY;
            box.style.transform = `translate(${moveX}px,${moveY}px)`;
        })
        // box.addEventListener("touchmove",function(event){
        // box.style.transfrom=`translate(${event.touches[0].pageX}px,${event.touches[0].pageY}px)`;
        //         })
        //球停止移动的时候需要记录当前移动的位置
        let stopX = 0;
        let stopY = 0;
        box.addEventListener("touchend", function () {
            //保留移动之后结束的坐标
            stopX = moveX;
            stopY = moveY;
        })
    </script>
</body>

</html>